package com.dhcc.cdc.busi.controller;


import com.baomidou.mybatisplus.plugins.Page;
import com.dhcc.cdc.busi.entity.CdcLocationEntity;
import com.dhcc.cdc.busi.entity.CdcRoomEntity;
import com.dhcc.cdc.busi.service.ICdcLocationService;
import com.dhcc.cdc.busi.service.ICdcRoomService;
import com.dhcc.core.framework.annotion.BussinessLog;
import com.dhcc.core.framework.annotion.Permission;
import com.dhcc.core.framework.base.controller.BaseController;
import com.dhcc.core.framework.base.warpper.EmptyWarpper;
import com.dhcc.core.framework.exception.BizException;
import com.dhcc.core.framework.exception.BizExceptionEnum;
import com.dhcc.core.framework.result.R;
import com.dhcc.core.framework.result.page.PageFactory;
import com.dhcc.core.framework.util.CommonUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;


/**
 * 病房
 * 
 * @ClassName: CdcRoomController
 * @Description: TODO
 * @author jumper
 * @date 2021-03-15 10:20:07
 */
@Controller
@RequestMapping("${sys.backendPath}/cdc/cdcroom")
public class CdcRoomController extends BaseController {

	private String PREFIX = "/backend/cdc/cdcroom/";

	@Autowired
	private ICdcRoomService cdcRoomService;
    @Autowired
    private ICdcLocationService cdcLocationService;
	

    /**
     * 跳转到病房列表首页
     */
    @RequestMapping("")
    @Permission("cdcroom:list")
    public String index() {
        return PREFIX + "cdcroom.html";
    }
	
    /**
     * 跳转到新增病房
     */
    @RequestMapping("/to_add")
    @Permission("cdcroom:add")
    public String toAdd() {
        return PREFIX + "cdcroom_add.html";
    }
	
	/**
     * 跳转到修改病房
     */
    @RequestMapping("/to_update/{cdcRoomId}")
    @Permission("cdcroom:update")
    public String toUpdate(@PathVariable Long cdcRoomId, Model model) {
        CdcRoomEntity cdcRoom = this.cdcRoomService.selectById(cdcRoomId);
        if(CommonUtil.isNotEmpty(cdcRoom.getLocationId())){
            CdcLocationEntity location = this.cdcLocationService.selectById(cdcRoom.getLocationId());
            cdcRoom.setLocation(location);
        }
        model.addAttribute("cdcRoom",cdcRoom);
        return PREFIX + "cdcroom_edit.html";
    }

    /**
     * 获取病房列表
     */
	@SuppressWarnings("unchecked")
    @RequestMapping(value = "/list")
    @ResponseBody
    @Permission("cdcroom:list")
    public Object list(@RequestParam Map<String, Object> params) {
		Page<CdcRoomEntity> page = new PageFactory<CdcRoomEntity>().defaultPage();
        List<CdcRoomEntity> result = this.cdcRoomService.page(page,params);
        page.setRecords((List<CdcRoomEntity>) new EmptyWarpper(result).warp());
        return R.packForBT(page);
    }

    @RequestMapping(value = "/query/{locationID}")
    @ResponseBody
    @Permission("cdclocation:list")
    public Object query(@PathVariable(required = false) Long locationID,@RequestParam Map<String, Object> params) {
        if(params.containsKey("q")) params.put("description", params.get("q"));
        if(CommonUtil.isNotEmpty(locationID)) params.put("locationId", locationID);
        List<CdcRoomEntity> result = this.cdcRoomService.list(params);
        return result;
    }

    /**
     * 新增病房
     */
    @RequestMapping(value = "/add")
    @ResponseBody
    @Permission("cdcroom:add")
    @BussinessLog(value = "新增病房")
    public Object add(CdcRoomEntity cdcRoom) {
        if (CommonUtil.isOneEmpty(cdcRoom)) {
            throw new BizException(BizExceptionEnum.REQUEST_NULL);
        }
        cdcRoom.insert();
        return R.ok();
    }

    /**
     * 删除病房
     */
    @RequestMapping(value = "/delete")
    @ResponseBody
    @Permission("cdcroom:delete")
    @BussinessLog(value = "删除病房")
    public Object delete(@RequestParam Long cdcRoomId) {
	
        this.cdcRoomService.deleteById(cdcRoomId);

        return R.ok();
    }

    /**
     * 修改病房
     */
    @RequestMapping(value = "/update")
    @ResponseBody
    @Permission("cdcroom:update")
    @BussinessLog(value = "修改病房")
    public Object update(CdcRoomEntity cdcRoom) {
        if (CommonUtil.isOneEmpty(cdcRoom)) {
            throw new BizException(BizExceptionEnum.REQUEST_NULL);
        }
        CdcRoomEntity old = this.cdcRoomService.selectById(cdcRoom.getId());

        old.setXcode(cdcRoom.getXcode());
        old.setCode(cdcRoom.getCode());
        old.setDescription(cdcRoom.getDescription());
        old.setLocationId(cdcRoom.getLocationId());
        old.setBusRoomTypeId(cdcRoom.getBusRoomTypeId());
        old.setDataId(cdcRoom.getDataId());
        old.setIsActive(cdcRoom.getIsActive());
        old.updateAllColumnById();
        return R.ok();
    }
	
}
